package com.lsx.logisticscore.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsx.logistics.response.CommonResp;
import com.lsx.logisticscore.entity.News;
import com.lsx.logisticscore.mapper.NewsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author: Liang
 * @Date: 2022/02/10
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsMapper newsMapper;

    @RequestMapping("/getNews")
    public CommonResp<Page<News>> getNews(Page<News> page){
        log.info("{},{}",page.getCurrent(),page.getSize());
        return new CommonResp<>(newsMapper.selectNews(page));
    }

    @RequestMapping("/getNewsById")
    public CommonResp<News> getNewsById(Integer id){
        return new CommonResp<>(newsMapper.selectNewsById(id));
    }

    @RequestMapping("/addNews")
    public CommonResp<String> addNews(@RequestBody News news){
        news.setCreateTime(new Date());
        news.setUpdateTime(new Date());
        newsMapper.insertSelective(news);
        return new CommonResp<>(news.getId().toString());
    }
}
